﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Projecturo.ProjecturoClient.Command;
using System.Windows.Input;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows.Data;
using System.ComponentModel;

namespace Projecturo.ProjecturoClient.ViewModel
{
    public class WorkspaceViewModel : ViewModelBase
    {
        private DelegateCommand openInWorkspaceCommand;

        public ICommand OpenInWorkspaceCommand
        {
            get
            {
                if (this.openInWorkspaceCommand == null)
                {
                    this.openInWorkspaceCommand = new DelegateCommand(OnOpenInWorkspaceRequest);
                }
                return this.openInWorkspaceCommand;
            }
        }

        public event EventHandler OpenInWorkspaceRequested;

        private void OnOpenInWorkspaceRequest()
        {
            EventHandler handler = OpenInWorkspaceRequested;
            if (handler != null)
            {
                handler(this, EventArgs.Empty);
            }
        }

        protected void AddToWorkspace(WorkspaceViewModel viewModel, ViewModelReference reference, WorkspaceViewModel originalViewModel)
        {
            if (viewModel == null)
                return;

            if (reference != null && reference.ParentViewModelId != null)
            {
                ViewModelBase viewModelBase = ViewModelManager.Instance.GetViewModel(reference.ParentViewModelId);
                if (viewModelBase is IWorkspace)
                {
                    (viewModelBase as IWorkspace).AddWorkspace(originalViewModel, true);
                }
                else
                {
                    AddToWorkspace(viewModelBase as WorkspaceViewModel, viewModelBase.ViewModelReference, originalViewModel);
                }
            }
        }
    }
}
